﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Security.Cryptography;
namespace BuyTicket
{
    /// <summary>
    /// 通用辅助类
    /// </summary>
    public static class Utility
    {
        public static string GetUtcTime()
        {
            TimeSpan tspan = DateTime.Now - Convert.ToDateTime("1970-01-01");
            Int64 interval = Convert.ToInt64((tspan.TotalMilliseconds)) - 8 * 3600;
            return interval.ToString();
        }

        public static string Md5(string str)
        {
            return BitConverter.ToString(MD5CryptoServiceProvider.Create().ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-","").ToLower();
        }

        public static string HttpEncode(string str)
        {
            return HttpUtility.UrlEncode(str);
        }

        /// <summary>
        /// UrlEncode
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>UrlEncode字符串</returns>
        public static string UrlEncode(string str)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.Default.GetBytes(str);
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(@"%" + Convert.ToString(byStr[i], 16));
            }
            return (sb.ToString());
        }

        /// <summary>
        /// 字符串分割查找
        /// </summary>
        /// <param name="src">源字符串</param>
        /// <param name="before">目标串前面的标识</param>
        /// <param name="after">目标串后面的标识</param>
        /// <returns>目标字符串或空</returns>
        public static string SplitString(string src,string before,string after)
        {
            string[] strArry = Regex.Split(src, before);
            if(strArry.Length>1)
            {
                strArry = Regex.Split(strArry[1], after);
                if(strArry.Length>=1)
                {
                    return strArry[0];
                }
            }
            return string.Empty;
        }
    }
}
